<?php 
class loginAction extends sfAction{
	public function execute($request){
		$form = new LoginForm();
		$errors = array();
		if($request->isMethod('POST')){
			$content = '';
			$login_counter = $this->getUser()->getAttribute('admin.login_counter',0);
			$data = $request->getParameter($form->getName());
			if($login_counter < intval(sfConfig::get('app_login_maxLogin',3))){
				$login_counter++;
				$form->bind($data);
				if($form->isValid()){
					$sys_account = SysAccounts::login($data);
					if(count($sys_account)){
						if(SysAccountIpLockeds::checkIPLockeds()){
							$this->getUser()->setAttribute('admin.accounts', $sys_account);
							$this->getUser()->setAttribute('admin.login_counter',null);
							SysLogs::insertLogs(array('content'=>'Login success', 'login_user' => $data['login_user']));
							$this->redirect(sfConfig::get('app_prefix').$this->generateUrl('preview_index'));
						}else{
							$content = 'Login failed. IP locked.';
						}
					}else{
						$content = 'Login failed. Invalid username/password.';
					}
				}else{
					$content = 'Login failed. Invalid username/password.';
				}
			}else{
				$content = 'Login failed. Over max login times ('.sfConfig::get('app_login_maxLogin',3).' times).';
			}
			SysLogs::insertLogs(array('content'=>$content, 'login_user'=>$data['login_user']));
			$errors['login_over'] = true;
			$this->getUser()->setAttribute('admin.login_counter',$login_counter);
		}
		$this->setVar('form', $form);
		$this->setVar('errors', $errors);
	}
}